package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysRole;
import com.wwu.system.service.ISysRoleService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 *  角色前端控制器
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Api(tags = "系统角色控制器(SysRoleController)")
@ApiSort(2)
@Controller
@RequestMapping("/system/sysRole")
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;

    /**
     * 请求转发角色管理主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/2 16:31
     */
    @GetMapping("/roleMain")
    public String roleMainHtml() {
        return "system/sysrole/role-main";
    }

    /**
     * 请求系统角色新增或更新页面
     * @param roleId
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/12 20:10
     */
    @GetMapping("/roleAddOrUpdate")
    public String roleAddOrUpdateHtml(String roleId,Model model){
        if(StringUtils.hasText(roleId)){
            model.addAttribute(CommConst.SYS_ROLE,sysRoleService.getById(roleId));
        }
        return "system/sysrole/role-add-edit";
    }

    /**
     * 请求系统角色授权新增或更新页面
     * @param roleId
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/12 20:10
     */
    @GetMapping("/roleMenuAddOrUpdate")
    public String roleMenuAddOrUpdateHtml(String roleId, Model model){
        if(StringUtils.hasText(roleId)){
            model.addAttribute(CommConst.SYS_ROLE,sysRoleService.getById(roleId));
        }
        return "system/sysrole/role-menu-edit";
    }

    @Log(menuName = "角色管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "分页查询角色信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysRole", value = "系统角色实体")
    })
    @GetMapping("/roleList")
    @ResponseBody
    public ResultPage<SysRole> roleList(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5000") Integer limit,
                                        SysRole sysRole){
        return sysRoleService.getRoleListPage(page,limit,sysRole);
    }

    @Log(menuName = "角色管理",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加角色信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveRole")
    @ResponseBody
    public  ResultObj saveRole(@RequestBody SysRole sysRole){
        sysRoleService.saveRole(sysRole);
        return ResultObj.success("角色信息添加成功");
    }

    @Log(menuName = "角色管理",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新角色信息")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateRole")
    @ResponseBody
    public ResultObj updateRole(@RequestBody SysRole sysRole){
        sysRoleService.updateRole(sysRole);
        return ResultObj.success("角色信息更新成功");
    }

    @Log(menuName = "角色管理",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除角色信息")
    @ApiOperationSupport(order = 4)
    @DeleteMapping("/deleteRole/{roleIds}")
    @ResponseBody
    public ResultObj deleteRole(@ApiParam(name = "roleIds", value = "角色id集合，以逗号分隔", required = true)
                                    @PathVariable String roleIds){
        sysRoleService.deleteRole(Arrays.asList(roleIds.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("角色信息删除成功");
    }
}